const Biz = require('../biz/index');
const DB = require('../db/index');
const exception = require('../exception');
const fw = require('../core/framework');
const camera = require('../component/camera');

function setDeviceTimestamp(successFn,errorFn){
    Biz.Config.save({code:'device',value:new Date().getTime(),name:'设备变化时间戳',remark:'识别设备是否有变化'},function(){
        if(fw.isFn(successFn))successFn();
    },function(){
        if(fw.isFn(errorFn))errorFn();
    })
}


module.exports = {
    init(router,urlencodedParser){
        router.post('/config/create', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                let data = {
                    name:param.name,
                    code:param.code,
                    value:param.value,
                    remark:param.remark,
                    merchant_id:fw.parseMerchantId(req)
                };
                Biz.Config.create(data,(r)=>{
                    res.send(r);
                },(r)=>{
                    res.send(r);
                });
            });
        });
        router.post('/config/update', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                if(fw.isExit(param.id)){
                    Biz.Config.update(param,(data)=>{
                        res.send(data);
                    },(data)=>{
                        res.send(data);
                    });
                }else{
                    res.send(exception.paramEx('id'));
                }
            });
        });
        router.post('/config/delete', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                console.log('执行删除配置业务');
                let param = fw.utils.parseBody(req);
                if(fw.isExit(param.id)){
                    Biz.Config.delete(param,(data)=>{
                        res.send(data);
                    },(data)=>{
                        res.send(data);
                    });
                }else{
                    res.send(exception.paramEx('id'));
                }
            });
        });
        router.post('/config/read', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                if(fw.isExit(param.id)){
                    // param.merchant_id = fw.parseMerchantId(req);
                    Biz.Config.read(param,(data)=>{
                        res.send(data);
                    },(data)=>{
                        res.send(data);
                    });
                }else{
                    res.send(exception.paramEx('id'));
                }
            });
        });
        router.post('/config/findByCode', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                if(fw.isExit(param.code)){
                    // param.merchant_id = fw.parseMerchantId(req);
                    Biz.Config.findByCode(param.code,(data)=>{
                        res.send(data);
                    },(data)=>{
                        res.send(data);
                    });
                }else{
                    res.send(exception.paramEx('code'));
                }
            });
        });
    }
};
